<template>
  <div class="base-map-resource-content">
    <div class="blacklist-tab">
      <div
        :class="['tab-item', tabActive === item.value ? 'tab-active' : '']"
        v-for="(item, index) in tabList"
        :key="index"
        @click="handleTabClick(item)"
      >
        {{ item.label }}
      </div>
    </div>
    <div class="query-content">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="用户名称">
          <el-input v-model="formInline.userName" @change="onSubmit" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="formInline.userPhone" @change="onSubmit" />
        </el-form-item>
        <!--        <el-form-item>-->
        <!--          <el-button type="primary" class="queryButton" @click="onSubmit"-->
        <!--            >查询</el-button-->
        <!--          >-->
        <!--          <el-button type="primary" class="resetButton" @click="onReset"-->
        <!--            >重置</el-button-->
        <!--          >-->
        <!--        </el-form-item>-->
      </el-form>
    </div>
    <div class="table-content">
      <div class="table">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column align="center" label="序号" width="180">
            <template #default="scope">
              {{
                scope.$index +
                1 +
                formInline.pageSize * (formInline.pageIndex - 1)
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="登录名"
            prop="loginName"
            show-overflow-tooltip
          />
          <el-table-column label="用户名称" show-overflow-tooltip>
            <template #default="scope">
              <span class="userName">
                {{ scope.row.userName }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="userPhone" label="用户手机号码" />
          <el-table-column prop="areaName" label="用户所属区域" />
          <el-table-column
            prop="lastLoginTime"
            label="最后登录时间"
            show-overflow-tooltip
          >
            <template #default="scope">
              {{
                formatDate(
                  new Date(scope.row.lastLoginTime),
                  'YYYY-MM-DD HH:mm:ss'
                )
              }}
            </template>
          </el-table-column>
          <!--          <el-table-column prop="userState" label="用户状态" show-overflow-tooltip>-->
          <!--            <template #default="scope">-->
          <!--              <span v-if="scope.row.userState == '1'">在线</span>-->
          <!--              <span v-else style="color: #ff455f">离线</span>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="操作">
            <template #default="scope">
              <el-button
                text
                type="text"
                style="padding: 0"
                @click="handleEditClick(scope.row)"
                v-if="currentUser?.hasPermission('hmdglmove')"
              >
              {{ currentUser?.getButtonName('hmdglmove') }}
              </el-button>
              <el-button
                text
                type="text"
                style="padding: 0"
                @click="handleLoginDetailClick(scope.row)"
                v-if="currentUser?.hasPermission('hmdgldetail')"
              >
              {{ currentUser?.getButtonName('hmdgldetail') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        background
        layout="slot, prev, pager, next"
        :page-size="formInline.pageSize"
        :current-page="formInline.pageIndex"
        :total="total"
        @current-change="handlePageChange"
      >
        <template #default> 共{{ total }}个用户 </template>
      </el-pagination>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      title="提示"
      align-center
      class="outline-dialog"
      width="30%"
    >
      <span
        >确认将用户“{{ userInfo.userName }}（{{
          userInfo.userPhone ? userInfo.userPhone : ''
        }}）”移出黑名单吗？</span
      >
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleUserOutline">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <login-detail-dialog
      :visible="loginDialogVisible"
      :user-info="userInfo"
      @closeDialog="handleDialogClick"
    ></login-detail-dialog>
  </div>
</template>

<script setup lang="ts">
import { getCurrentInstance, onMounted, reactive, ref } from 'vue'
import { queryUserList, updateUser } from '@/api/back-management/user'
import { formatDate } from '@/utils'
import { Message as ElMessage } from 'element-ui'
import type { OptionData } from '@/utils/type'
import type { UserInfoResponse } from '@/api/back-management/user/interface'
import LoginDetailDialog from '@/views/backend-management/components/blacklist/components/loginDetailDialog.vue'

const vm = getCurrentInstance()
const store = vm?.proxy?.$store

const currentUser = store?.state?.user?.currentUser

const tabActive = ref('0')

const loginDialogVisible = ref(false)

const tabList: OptionData[] = [
  {
    label: '黑名单用户',
    value: '0',
  },
  {
    label: '黑名单IP',
    value: '1',
  },
]

const formInline = reactive({
  pageIndex: 1,
  pageSize: 11,
  userName: '',
  userState: '2',
  userPhone: '',
})

const total = ref(0)

const dialogVisible = ref(false)

const tableData = ref<UserInfoResponse[]>([])
const userInfo = reactive<Partial<UserInfoResponse>>({})
const getAbnormalUserList = () => {
  queryUserList(formInline).then((res) => {
    if (res.success) {
      total.value = res.data.count
      console.log(res.data.list)
      tableData.value = res.data.list
    }
  })
}

onMounted(() => {
  getAbnormalUserList()
})

const onSubmit = () => {
  formInline.pageIndex = 1
  getAbnormalUserList()
}

const onReset = () => {
  formInline.pageIndex = 1
  formInline.userPhone = ''
  formInline.userName = ''
  getAbnormalUserList()
}

const handleEditClick = (item: UserInfoResponse) => {
  Object.assign(userInfo, { ...item })
  dialogVisible.value = true
}

const handleLoginDetailClick = (item: UserInfoResponse) => {
  Object.assign(userInfo, { ...item })
  loginDialogVisible.value = true
}

const handleUserOutline = () => {
  updateUser(userInfo.pkid!, { userState: 1 })
    .then((res) => {
      if (res.success) {
        getAbnormalUserList()
        dialogVisible.value = false
        ElMessage.success('更新用户成功！')
      }
    })
    .catch((e) => {
      ElMessage.error(e)
    })
}

const handlePageChange = (index: number) => {
  formInline.pageIndex = index
  getAbnormalUserList()
}

const handleTabClick = (item: OptionData) => {
  tabActive.value = item.value
}

const handleDialogClick = () => {
  loginDialogVisible.value = false
}
</script>

<style lang="scss" scoped>
.base-map-resource-content {
  width: 100%;
  height: calc(100% - 27px);
  background-color: white;
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.1);
  border-radius: 2px;

  .blacklist-tab {
    padding-left: 20px;
    width: 100%;
    height: 62px;
    border-bottom: 1px solid #eeeff5;
    display: flex;
    align-items: center;

    .tab-item {
      cursor: pointer;
      width: 100px;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 12px;
      position: relative;
    }

    .tab-item.tab-active {
      color: #28b1e4;
      font-weight: 500;
      &::after {
        position: absolute;
        content: ' ';
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 60%;
        height: 3px;
        background-color: #28b1e4;
      }
    }
  }

  .query-content {
    padding-left: 20px;
    width: 100%;
    height: 62px;
    line-height: 62px;
    border-bottom: 1px solid #eeeff5;

    :deep(.el-form) {
      .el-form-item {
        margin-bottom: 0;

        .resetButton {
          width: 80px;
          height: 32px;
          color: #28b1e4;
          background: #ffffff;
          border: 1px solid #28b1e4;
          border-radius: 2px;
        }

        .queryButton {
          width: 80px;
          height: 32px;
          background: #28b1e4;
          border-radius: 2px;
        }
      }
    }
  }

  .table-content {
    width: 100%;
    height: calc(100% - 63px - 63px);

    .table {
      margin-top: 12px;
      width: 100%;
      padding: 0 18px;
      height: calc(100% - 77px - 28px);
      overflow-y: auto;

      .userName {
        .item {
          margin-left: 6px;
        }
        :deep(.el-icon) {
          vertical-align: sub;

          .svg-icon {
            svg {
              width: 16px;
              height: 16px;
              color: #28b1e4;
            }
          }
        }
        :deep(.el-badge__content) {
          width: 12px;
          height: 13px;
          transform: translateY(85%) translateX(148%);
        }
      }

      :deep(.el-table__row--striped) {
        background: #f7f9fb;
      }
    }

    .el-pagination {
      margin-top: 25px;
      justify-content: end;
      margin-right: 18px;
    }
  }
}
</style>
